import config from '@/config.js'
import {
	getStorage
} from '@/util/auth.js';
import {
	Encrypt,
	Decrypt
} from '@/js_sdk/cainiao-crypto/cainiao-crypto.js'
import {
	getWxLoginCode
} from '@/util/user.js'
import store from '@/store/index.js'
export default function(parameter) {
	parameter.header = {};
	const token = getStorage('token');
	const position = getStorage('position');
	if (token) {
		parameter.header['token'] = token;
	}
	if (position) {
		parameter.header['latitude'] = position.latitude;
		parameter.header['longitude'] = position.longitude;
	}
	parameter.url = config.requestUrl + parameter.url
	return new Promise((resolve, reject) => {
		uni.request({
			...parameter,
			success(e) {
				uni.hideLoading().then(() => {
					if (e.statusCode != 200) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							mask: true,
						})
						reject();
					} else {
						const response = e.data;
						switch (response.code) {
							case 200:
							//本地调试时，可以删除此方法
							if (response.data) {
								let deStr;
								const mi = Decrypt(response.data);
								try {
									deStr = JSON.parse(mi);
								} catch (error) {
							
									deStr = mi; // 或者其他默认值
								}
								resolve(deStr);
								break;
							}
							resolve(response.data);
							break;
							case 401:
								uni.showToast({
									title: '登录信息已过期，请重新登录',
									icon: 'none',
									mask: true,
									duration: 2000,
									success: () => {
										setTimeout(() => {
											getWxLoginCode().then((
												code) => {
												uni.showLoading({
													title: '加载中',
													mask: true,
												})
												store.dispatch(
													'user/getWxLoginCode',
													code).catch(
													() => {
														uni.showModal({
															title: '提示',
															content: '获取信息失败，请尝试重新进入小程序，否则功能无法正常使用',
														})
													})
											})
										}, 2000)
									}
								})
								break;
							case 403:
								uni.showToast({
									title: '暂无权限',
									icon: 'none',
									duration: 2000,
									mask: true,
									success() {
										setTimeout(() => {
											reject();
										}, 2000)
									}
								})
								break;
							case 500:
								uni.showToast({
									title: response.msg,
									icon: 'none',
									duration: 2000,
									mask: true,
									success() {
										setTimeout(() => {
											reject();
										}, 2000)
									}
								})
								break;
							default:
								uni.showToast({
									title: response.msg,
									icon: 'none',
									duration: 2000,
									mask: true,
									success() {
										setTimeout(() => {
											reject();
										}, 2000)
									}
								})
								break;
						}
					}
				})

			},
			fail() {
				uni.hideLoading().then(() => {
					uni.showToast({
						title: '请求失败',
						icon: 'none',
						mask: true,
						duration: 2000,
						success() {
							setTimeout(() => {
								reject();
							}, 2000)
						}
					})
				})
			}
		})
	})
}